package com.jwthhealth_pub.old.subscribe.adapter;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.jwthhealth_pub.old.subscribe.SubscribeInfoActivity;
import com.jwthhealth_pub.old.subscribe.SubscribeListActivity;
import com.jwthhealth_pub.old.subscribe.model.SubscribeList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * subscribeList 列表
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeListAdapter extends RecyclerView.Adapter {
    private final SubscribeListActivity mActivity;

    private List<SubscribeList.DataBean> data;

    private int clickIndex = -1;

    public SubscribeListAdapter(SubscribeListActivity subscribeListActivity, List<SubscribeList.DataBean> data) {
        this.mActivity = subscribeListActivity;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mActivity, R.layout.item_subscribe, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder vh = (ViewHolder) holder;
        final SubscribeList.DataBean dataBean = data.get(position);

        if (position == clickIndex) {
            vh.infoLayout.setVisibility(View.VISIBLE);
            vh.btnUnfold.setBackgroundResource(R.mipmap.btn_subscribe_up);
        } else {
            vh.infoLayout.setVisibility(View.GONE);
            vh.btnUnfold.setBackgroundResource(R.mipmap.btn_subscribe_down);
        }

        ImageLoader.picasso(dataBean.getPic(), vh.img);
        vh.shopName.setText(dataBean.getName());
        vh.shopProject.setText("预约项目:" + dataBean.getPname() + "");
        vh.shopDate.setText("预约日期:" + dataBean.getAp_date() + "");
        vh.shopTime.setText("预约时间:" + dataBean.getAtime() + "");
        vh.shopIdentifyName.setText("预约人:" + dataBean.getAname() + "");
        vh.shopIdentifyPhone.setText("联系电话:" + dataBean.getAphone() + "");
        vh.shopIdentifyId.setText("身份证号:" + dataBean.getNo_card() + "");
        vh.shopIdentifyShopphone.setText("客服电话:" + dataBean.getKf_phone() + "");
        vh.shopIdentifyAdd.setText("地址:" + dataBean.getPos() + "");
//        vh.gotoLayout.setVisibility(ViewHolder.VISIBLE);

        Resources resources = mActivity.getResources();
        switch (dataBean.getAstatus()) {
            case "1":
                //正在审核
                vh.shopState.setText(resources.getString(R.string.subscribe_state_one));
                vh.shopState.setBackgroundResource(R.mipmap.subscribe_status_one);
                vh.gotoLayout.setVisibility(View.GONE);
                vh.optLayout.setVisibility(View.VISIBLE);
                vh.cancelBtn.setVisibility(View.VISIBLE);
                vh.modifyBtn.setVisibility(View.VISIBLE);
                break;
            case "2":
                //预约成功
                vh.shopState.setText(resources.getString(R.string.subscribe_state_two));
                vh.shopState.setBackgroundResource(R.mipmap.subscribe_status_two);
                vh.optLayout.setVisibility(View.VISIBLE);
                vh.cancelBtn.setVisibility(View.VISIBLE);
                vh.modifyBtn.setVisibility(View.GONE);
                break;
            case "3":
                //已完成
                vh.shopState.setText(resources.getString(R.string.subscribe_state_thi));
                vh.shopState.setBackgroundResource(R.mipmap.subscribe_status_thi);
                vh.gotoLayout.setVisibility(View.GONE);
                vh.modifyBtn.setText(mActivity.getString(R.string.subscribe_commit_again));
                vh.optLayout.setVisibility(View.GONE);
//                vh.modifyBtn.setVisibility(ViewHolder.GONE);
//                vh.cancelBtn.setVisibility(ViewHolder.GONE);
                break;
            case "4":
                //已取消
                vh.shopState.setText(resources.getString(R.string.subscribe_state_for));
                vh.shopState.setBackgroundResource(R.mipmap.subscribe_status_for);
                vh.gotoLayout.setVisibility(View.GONE);
                vh.modifyBtn.setText(mActivity.getString(R.string.subscribe_commit_again));
                vh.optLayout.setVisibility(View.VISIBLE);
                vh.modifyBtn.setVisibility(View.VISIBLE);
                vh.cancelBtn.setVisibility(View.GONE);
                break;
            default:
                break;
        }

        final int pos = vh.getAdapterPosition();

        vh.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(mActivity, SubscribeInfoActivity.class);
//                intent.putExtra(Constant.DATA, dataBean);
//                mActivity.startActivity(intent);

                if (clickIndex == pos) {
                    clickIndex = -1;
//                    vh.btnUnfold.setBackgroundResource(R.mipmap.btn_subscribe_down);
                } else {
                    clickIndex = pos;
//                    vh.btnUnfold.setBackgroundResource(R.mipmap.btn_subscribe_up);
                }
                notifyDataSetChanged();
            }
        });

        vh.btnInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, SubscribeInfoActivity.class);
                intent.putExtra(Constant.DATA, dataBean);
                intent.putExtra("cancancel", true);
                intent.putExtra("ishintshow", true);
                mActivity.startActivity(intent);
            }
        });

        vh.cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //todo 取消预约
                mActivity.getalertDialog(false, data.get(pos));
            }
        });

        vh.modifyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: 2017/10/12 修改预约, 重新跳转到预约页面
                if (dataBean.getAstatus().equals("4"))
                    mActivity.reSubscibe(data.get(pos));
                else
                    mActivity.getalertDialog(true, data.get(pos));

            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.shop_name)
        TextView shopName;
        @BindView(R.id.shop_project)
        TextView shopProject;
        @BindView(R.id.shop_date)
        TextView shopDate;
        @BindView(R.id.shop_time)
        TextView shopTime;
        @BindView(R.id.shop_identify_name)
        TextView shopIdentifyName;
        @BindView(R.id.shop_identify_phone)
        TextView shopIdentifyPhone;
        @BindView(R.id.shop_identify_id)
        TextView shopIdentifyId;
        @BindView(R.id.shop_identify_shopphone)
        TextView shopIdentifyShopphone;
        @BindView(R.id.shop_identify_add)
        TextView shopIdentifyAdd;
        @BindView(R.id.btn_gotoinfo)
        ImageView btnInfo;
        @BindView(R.id.shop_state)
        TextView shopState;
        @BindView(R.id.layout_moreinfo)
        LinearLayout infoLayout;
        @BindView(R.id.click_layout)
        CardView clickLayout;
        @BindView(R.id.goto_layout)
        RelativeLayout gotoLayout;
        @BindView(R.id.btn_unfold)
        ImageView btnUnfold;
        @BindView(R.id.cancel_btn)
        Button cancelBtn;
        @BindView(R.id.modify_btn)
        Button modifyBtn;
        @BindView(R.id.opt_layout)
        RelativeLayout optLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
